<?php

/**
 * estudio actions.
 *
 * @package    espazio
 * @subpackage estudio
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class estudioActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    // Obtener el MENU ESTUDIOS zona izquierda
    // 1.- Obtener la familia del estudio
    $c = new Criteria();
    $c -> add(EstudioPeer::ID,$request->getParameter('id'));    
    $this->estudio = EstudioPeer::doSelect($c);
    $estudio = $this->estudio;
    $id= $estudio[0]->getFamiliaId();
    //print_r($this->estudio[0]->getFamiliaId());

    $this->titulo = FamiliaPeer::retrieveByPk($id);
     //print_r($this->titulo->getNombre());
   // echo $id;
    // 2.- Obtengo todos los hijos que tienen el padre de
    $c = new Criteria();
    $c -> add(EstudioPeer::FAMILIA_ID,$id);
    $this->familia = EstudioPeer::doSelect($c);
   // print_r($this->familia);

    // Obtengo todos los centros
    $c = new Criteria();
    $c -> add(CentroEstudioPeer::ESTUDIO_ID,$request->getParameter('id'));
    $this->listado_centros = CentroEstudioPeer::doSelectJoinCentro($c);
    //print_r($this->estudio_list);

  }

  public function executeShow(sfWebRequest $request)
  {
    $this->estudio = EstudioPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->estudio);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new EstudioForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new EstudioForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($estudio = EstudioPeer::retrieveByPk($request->getParameter('id')), sprintf('Object estudio does not exist (%s).', $request->getParameter('id')));
    $this->form = new EstudioForm($estudio);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($estudio = EstudioPeer::retrieveByPk($request->getParameter('id')), sprintf('Object estudio does not exist (%s).', $request->getParameter('id')));
    $this->form = new EstudioForm($estudio);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($estudio = EstudioPeer::retrieveByPk($request->getParameter('id')), sprintf('Object estudio does not exist (%s).', $request->getParameter('id')));
    $estudio->delete();

    $this->redirect('estudio/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $estudio = $form->save();

      $this->redirect('estudio/edit?id='.$estudio->getId());
    }
  }
}
